'use strict'

module.exports = app => ({
  /**
   * h5 banner
   */
  async getH5Banner () {
    const { ctx, service } = app
    const result = await service.home.getH5Banner()
    const res = {
      list: result.length > 0 ? result.map(it => ({
        id: it.banner_id,
        imageUrl: it.banner_url,
        desc: it.desc,
        sort: it.sort,
        status: it.status,
        createTime: it.createdAt,
        updateTime: it.updatedAt
      })) : []
    }
    ctx.helper.success({ data: res })
  },

  /**
   * 首页轮播图
   */
  async getAdminBanner () {
    const {
      ctx,
      service
    } = app
    const req = ctx.request.query
    const result = await service.home.getAdminBanner(req)
    const res = {
      list: result.data.length > 0 ? result.data.map(it => ({
        bannerId: it.banner_id,
        imageUrl: it.banner_url,
        desc: it.desc,
        sort: it.sort,
        status: it.status,
        createTime: it.createdAt,
        updateTime: it.updatedAt
      })) : [],
      pageData: result.pageData
    }
    ctx.helper.success({ data: res })
  },

  /**
   * admin轮播图新增/编辑
   */
  async updateHomeBanner () {
    const {
      ctx,
      service
    } = app
    const data = ctx.request.body
    try {
      ctx.validate({
        imageUrl: 'string',
        desc: 'string',
        sort: 'number'
      })
    } catch (error) {
      ctx.throw(400, '请求参数有误')
    }
    await service.home.updateHomeBanner(data)
    ctx.helper.success()
  },

  /**
   * h5首页
   */
  async h5home () {
    const { ctx, service } = app
    const result = await service.home.h5home()
    ctx.helper.success()
  },

  /**
   * admin轮播图状态修改
   */
  async updateBannerStatus () {
    const {
      ctx,
      service
    } = app
    const data = ctx.request.body
    ctx.validate({
      bannerId: 'number',
      status: 'number'
    })
    await service.home.updateHomeBanner(data)
    ctx.helper.success()
  },

  /**
   * 轮播图删除
   */
  async deleteBanner () {
    const { ctx, service } = app
    const data = ctx.request.body
    await service.home.deleteBanner(data)
    ctx.helper.success()
  },

  /**
   * admin首页推荐列表
   */
  async getRecommendList () {
    const { ctx, service } = app
    const req = ctx.request.query
    const result = await service.home.getRecommendList(req)
    const res = {
      list: result.data.length > 0 ? result.data.map(it => ({
        recommendId: it.recommend_id,
        imageUrl: it.image_url,
        description: it.description,
        price: it.price,
        sort: it.sort,
        status: it.status,
        createTime: it.createdAt,
        updateTime: it.updatedAt
      })) : [],
      pageData: result.pageData
    }
    ctx.helper.success({ data: res })
  },

  /**
   * 推荐新增、编辑
   */
  async updateRecommend () {
    const { ctx, service } = app
    const data = ctx.request.body
    await service.home.updateRecommend(data)
    ctx.helper.success()
  },

  /**
   * 推荐详情
   */
  async recommendDetail () {
    const { ctx } = app
    const { recommendId } = ctx.request.query
    const result = await service.home.recommendDetail(recommendId)
    const { recommend_id, image_url, description, detail, price, sort, status } = result
    const res = {
      recommendId: recommend_id,
      imageUrl: image_url,
      description,
      detail,
      sort,
      price,
      status
    }
    ctx.helper.success({ data: res })
  },

  /**
   * 推荐删除
   */
   async deleteRecommend () {
    const { ctx, service } = app
    const data = ctx.request.body
    await service.home.deleteRecommend(data)
    ctx.helper.success()
  }
})